package com.ice.app.project.scheduleJob.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.project.entity.ScheduleJob;
import com.ice.app.project.scheduleJob.service.ScheduleJobService;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;

/**
 * 
 * 控制层： 定时任务表
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/scheduleJob")
public class ScheduleJobController extends BaseController {

	@Autowired
	private ScheduleJobService scheduleJobService;

	// --------------------------------------页面-----------------------------------------

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getScheduleJob(@RequestParam(value = "key") String key, HttpServletRequest request) {
		ScheduleJob scheduleJob = scheduleJobService.getScheduleJob(key);
		return Result.success().add("scheduleJob", scheduleJob);
	}

	// -----------------------------------数据---------------------------------------

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param scheduleJob
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result scheduleJobIndexData(ScheduleJob scheduleJob, Page<ScheduleJob> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<ScheduleJob> scheduleJobs = scheduleJobService.getScheduleJobs(scheduleJob);
		PageInfo<ScheduleJob> data = new PageInfo<ScheduleJob>(scheduleJobs, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param scheduleJob
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveScheduleJob(ScheduleJob scheduleJob) throws Exception {
		if (scheduleJobService.saveScheduleJob(scheduleJob)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delScheduleJob(String keys) {
		if (scheduleJobService.deleteScheduleJobs(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 停止任务
	 * 
	 * @param keys
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/stop", method = RequestMethod.POST)
	public Result stopScheduleJob(@RequestParam("key") String key) throws Exception {
		scheduleJobService.stopJob(key);
		return Result.success();
	}

	/**
	 * 激活任务
	 * 
	 * @param keys
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/active", method = RequestMethod.POST)
	public Result activeScheduleJob(@RequestParam("key") String key) throws Exception {
		scheduleJobService.activeJob(key);
		return Result.success();
	}

	/**
	 * 停止定时器
	 * 
	 * @param keys
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/shutdownJobs", method = RequestMethod.POST)
	public Result shutdownJobs() throws Exception {
		scheduleJobService.shutdownJobs();
		return Result.success();
	}

	/**
	 * 启动定时器
	 * 
	 * @param keys
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/startJobs", method = RequestMethod.POST)
	public Result startJobs() throws Exception {
		scheduleJobService.startJobs();
		return Result.success();
	}

}